package com.wing.storage.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.exception.BusinessException;
import com.wing.platform.order.model.entity.Warehouse;
import com.wing.product.model.entity.ProductDetail;
import com.wing.product.model.response.WarehouseAreaSkuItermVo;
import com.wing.product.model.response.WarehouseSkuExportVO;
import com.wing.product.service.ProductDetailService;
import com.wing.purchase.dao.WarehouseShelfSpaceDao;
import com.wing.purchase.model.entity.WarehouseShelfSpace;
import com.wing.storage.dao.WarehouseAreaSkuItermDao;
import com.wing.storage.model.entity.ProductInventory;
import com.wing.storage.model.entity.ProductInventoryDetail;
import com.wing.storage.model.entity.WarehouseAreaSkuIterm;
import com.wing.storage.model.response.WarehouseSkuItermVo;
import com.wing.visual.service.WarehouseService;
import com.wing.web.excel.WarehouseSkuItermExcelIn;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


/**
 * 库位存货明细-ServiceImpl
 *
 */
@Service
@Slf4j
public class WarehouseAreaSkuItermServiceImpl extends ServiceImpl<WarehouseAreaSkuItermDao,WarehouseAreaSkuIterm> implements WarehouseAreaSkuItermService {

    @Autowired
    private ProductDetailService productDetailService;
    @Autowired
    private WarehouseShelfSpaceDao warehouseShelfSpaceDao;
    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private ProductInventoryService productInventoryService;
    @Autowired
    private ProductInventoryDetailService productInventoryDetailService;
    
    @Override
    public Page<WarehouseSkuItermVo> list4app(Page page, Map<String, Object> params) {
        List<WarehouseSkuItermVo> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<WarehouseAreaSkuItermVo> getAreaSkuItermInfoBy(Long warehouseId) {
        return baseMapper.getAreaSkuItermInfoBy(warehouseId);
    }

    @Override
    public int updateStockSkuInfo(WarehouseSkuItermVo warehouseSkuItermVo) {
        WarehouseAreaSkuIterm warehouseAreaSkuIterm=new WarehouseAreaSkuIterm();
        BeanUtils.copyProperties(warehouseSkuItermVo,warehouseAreaSkuIterm);
        int counts= baseMapper.updateById(warehouseAreaSkuIterm);
        return counts>0?1:0;
    }


    @Override
    public List<WarehouseSkuExportVO> listExport(Map<String, Object> params) {
        return baseMapper.listExport(params);
    }

    @Override
    public List<WarehouseSkuItermVo> getSkuShelfSpaceCode(Map<String, Object> params) {
        return baseMapper.getSkuShelfSpaceCode(params);
    }

    private int add(WarehouseAreaSkuIterm warehouseAreaSkuIterm) {
        return baseMapper.insert(warehouseAreaSkuIterm);
    }
    
    @Override
    public void importExcel(List<WarehouseSkuItermExcelIn> list) {
        int i = 1;
        for (WarehouseSkuItermExcelIn skuItermExcelIn : list) {
            if (StringUtils.isBlank(skuItermExcelIn.getSkuCode())) {
                throw new BusinessException(300, "第" + i + "行【编码】未输入数据");
            }
            if (StringUtils.isBlank(skuItermExcelIn.getShelfSpaceCode())) {
                throw new BusinessException(300, "第" + i + "行【料架】未输入数据");
            }
            if (StringUtils.isBlank(skuItermExcelIn.getNum())) {
                throw new BusinessException(300, "第" + i + "行【数量】未输入数据");
            }
            if (StringUtils.isBlank(skuItermExcelIn.getWarehouseName())) {
                throw new BusinessException(300, "第" + i + "行【仓库】未输入数据");
            }
            WarehouseAreaSkuIterm warehouseAreaSkuIterm = new WarehouseAreaSkuIterm();
            //物料编码
            warehouseAreaSkuIterm.setSkuCode(skuItermExcelIn.getSkuCode());
            QueryWrapper<ProductDetail> productDetailQueryWrapper = new QueryWrapper();
            productDetailQueryWrapper.eq("sku_code", skuItermExcelIn.getSkuCode());
            ProductDetail productDetail = productDetailService.getOne(productDetailQueryWrapper);
            if (ObjectUtil.isNull(productDetail)) {
                throw new BusinessException("【" + skuItermExcelIn.getSkuCode() + "】" + "物料不存在");
            }
            //sku id
            warehouseAreaSkuIterm.setSkuId(productDetail.getId());
            //物料名称
            warehouseAreaSkuIterm.setSkuName(productDetail.getSkuName());
            //库位
            QueryWrapper<WarehouseShelfSpace> warehouseShelfSpaceQueryWrapper = new QueryWrapper();
            warehouseShelfSpaceQueryWrapper.eq("code", skuItermExcelIn.getShelfSpaceCode());
            WarehouseShelfSpace warehouseShelfSpace = warehouseShelfSpaceDao.selectOne(warehouseShelfSpaceQueryWrapper);
            if (ObjectUtil.isNull(warehouseShelfSpace)) {
                throw new BusinessException("【" + skuItermExcelIn.getShelfSpaceCode() + "】" + "料架不存在");
            }
            warehouseAreaSkuIterm.setWarehouseShelfSpaceId(warehouseShelfSpace.getId());
            //货架
            warehouseAreaSkuIterm.setWarehouseShelfId(warehouseShelfSpace.getShelfId());
            //库区
            warehouseAreaSkuIterm.setWarehouseAreaId(warehouseShelfSpace.getWarehouseAreaId());
            //数量
            warehouseAreaSkuIterm.setNum(BigDecimal.valueOf(Long.parseLong(skuItermExcelIn.getNum())));
            //仓库id
            QueryWrapper<Warehouse> warehouseQueryWrapper = new QueryWrapper();
            warehouseQueryWrapper.eq("name", skuItermExcelIn.getWarehouseName());
            Warehouse warehouse = warehouseService.getOne(warehouseQueryWrapper);
            if (ObjectUtil.isNull(warehouse)) {
                throw new BusinessException("【" + skuItermExcelIn.getWarehouseName() + "】" + "仓库不存在");
            }
            warehouseAreaSkuIterm.setWarehouseId(warehouse.getId());
            int result = add(warehouseAreaSkuIterm);
            if (result == 0) {
                throw new BusinessException("添加失败");
            }
            /**添加产品库存*/
            ProductInventory productInventory = new ProductInventory();
            productInventory.setProductDetailId(productDetail.getId());
            productInventory.setQuantity(warehouseAreaSkuIterm.getNum());
            productInventoryService.save(productInventory);
            //明细
            ProductInventoryDetail productInventoryDetail = new ProductInventoryDetail();
            productInventoryDetail.setProductDetailId(productDetail.getId());
            productInventoryDetail.setProductInventoryId(productInventory.getId());
            productInventoryDetail.setWarehouseTypeId(1L);
            productInventoryDetail.setWarehouseId(warehouse.getId());
            productInventoryDetail.setWarehouseAreaId(warehouseShelfSpace.getWarehouseAreaId());
            productInventoryDetail.setWarehouseShelfId(warehouseShelfSpace.getShelfId());
            productInventoryDetail.setWarehouseShelfSpaceId(warehouseShelfSpace.getId());
            productInventoryDetail.setQuantity(warehouseAreaSkuIterm.getNum());
            productInventoryDetailService.save(productInventoryDetail);
        }
    }


}
